<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 顶部搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" clearable @clear="getGoodsList" @blur="getGoodsList" v-model="queryData.query" class="input-with-select">
            <el-button @click="getGoodsList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <!-- dataFormat为在main.js中定义的 格式化日期的过滤器 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="80"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column label="创建时间" width="150">
          <template slot-scope="scope">
            {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="removeGoods(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品列表所需要的参数
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 存储接口返回的所有商品列表 总数
      goodsList: [],
      total: 0
    };
  },

  methods: {
    // 获取所有商品列表
    async getGoodsList() {
      var res = await this.axios.get("goods", {
        params: this.queryData
      });
      if (res.meta.status == 200) {
        this.goodsList = res.data.goods;
        this.total = res.data.total;
      }
      console.log(this.goodsList);
    },

    handleSizeChange(v) {
      this.queryData.pagesize = v;
      this.getGoodsList();
    },
    handleCurrentChange(v) {
      this.queryData.pagenum = v;
      this.getGoodsList();
    },

     //点击删除商品触发确认删除提示框
    removeGoods(id) {
      this.$cfm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          //确定发送请求删除用户
          var res = await this.axios.delete("goods/" + id);
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            //删除成功 重新渲染页面
            this.getGoodsList();
          } else {
            this.$message.error(res.meta.msg);
          }
        })
        .catch(() => {
          this.$message.info("取消删除");
        });
    },

    // 去往添加商品的页面
    addGood() {
      this.$router.push("/addGoods");
    }
  },

  created() {
    this.getGoodsList();
  }
};
</script>

<style lang='less' scoped>
</style>
